package server;

import java.net.*;
import java.io.*;

public class Server {

	private ServerSocket server;
	private Socket connessione;
	private BufferedReader dalClient;
	private PrintStream alClient;
	private String name;
	
	public Server(String name){
		this.name=name;
		try{
			server = new ServerSocket(1000,5);
			System.out.println("Server Attivo");
			connessione = server.accept();
			dalClient = new BufferedReader(new InputStreamReader(connessione.getInputStream()));
			alClient = new PrintStream(connessione.getOutputStream());
		}
		catch (IOException e){
			System.out.println(e);
			
		}
	} 
	
	public void conversazione(){
		String messaggio = "";
		BufferedReader t= new BufferedReader(new InputStreamReader(System.in));
		try{
			alClient.println("SimpleChat - Sei Connesso al Server! Digita logout per disconnetterti.");
			while(!messaggio.equals("/logout")){
				messaggio= dalClient.readLine();
				System.out.println(messaggio);
				if (!messaggio.equals("/logout")){
					messaggio=t.readLine();
					alClient.println(name+" scrive"+messaggio);
				}
			} //endwhile
		} //end try
		catch (IOException e){
			System.out.println(e);
		}
	} //end conversazione()
	
}

